import { ApiObject } from 'amis-core/lib/types'
import ApiObjectConfigProps from '@/components/types/ApiObjectConfigProps'
import { isNil, isUndefined } from 'lodash-es'
import serverApiUtils from '@/core/utils/serverApiUtils'
import lodash from 'lodash'

class ApiObjectConfigPropsUtils {
	convert = (serverApi: ApiObjectConfigProps): ApiObject | undefined => {
		if (isNil(serverApi)) {
			return undefined
		}
		let result: ApiObject = lodash.merge({} as ApiObject, serverApi)
		if (!isUndefined(serverApi.apiConfig)) {
			result.url = serverApiUtils.getUriOrMockUri(serverApi.apiConfig)
			result.method = serverApi.apiConfig.method as 'get' | 'post' | 'put' | 'delete' | 'patch' | 'jsonp' | 'js'
		}
		return result
	}
}

export default new ApiObjectConfigPropsUtils()
